# GNAFU - "Ground, Navy and Air Fuck-Up" real-time strategy game
# Copyright (C) 2008-2009 k0wax <k0waxk0wax@gmail.com>
#
# This file is part of GNAFU.
#
# GNAFU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GNAFU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNAFU.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import division, absolute_import
import yaml
import os, sys

vars = {}
data_path = os.path.join(os.path.dirname(__file__), '..', 'data')
default_conf = os.path.join(os.path.dirname(__file__), 'default.cfg')

def set_config(filename):
    read_config(filename)

def set_default_config():
    read_config(default_conf)

def read_config(filename):
    global vars
    #print os.getcwd()
    #print default_conf
    if not os.path.exists(filename):
        if os.path.exists(default_conf):
            print 'WARNING config not found, using default config from the source directory'
            filename = default_conf
        else:
            print 'Config file not found'
            sys.exit()
    if os.path.exists(filename):
        d = yaml.safe_load(open(filename))
        for k in d.keys():
            sec = d[k]
            sec_name = k
            for k, v in sec.items(): 
                vars['%s.%s' % (sec_name, k)] = v
            vars[sec_name] = sec # fixme

# XXX Warning: set default config by default, should be overriden manually
set_default_config()
